<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //什么是promise.or呢 就是 promise.or([p1,p2,p3,p4]) 若p1 失败 继续看p2 若p2成功返回p2 否则继续看p3 若p1p2p3p4都失败 则返回失败
        let p1 = Promise.reject(1);
        let p2 = Promise.reject(2);
        let p3 = Promise.reject(3);
        let p4 = Promise.resolve(4);

        Promise.or = function(promises) {
            let len = promises.length;
            let i = 0
            return new Promise((resolve, reject) => {
                help(resolve, reject);
            })

            function help(resolve, reject) {
                promises[i].then((res) => {
                    resolve(res);
                }).catch((err) => {
                    if (i == len - 1) return reject(err);
                    else {
                        i++;
                        help(resolve, reject)
                    }
                })
            }
        }
        Promise.or([p1, p2, p3, p4]).then((res) => {
            console.log(res);
        }, (err) => {
            console.log('cuowule');
        })
    </script>
</body>

</html>